/*
 * CAD System for seamlessly constructing 2D and 3D scenes with aspects and motion
 * Copyright (C) 2013 Bernhard Haumacher and others
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.haumacher.draw5d.dataflow.core;

import java.util.ArrayList;
import java.util.List;

import de.haumacher.draw5d.dataflow.Observable;
import de.haumacher.draw5d.dataflow.Scope;

/**
 * Base class for {@link Observable} implementations managing {@link Scope} and
 * {@link #getUsage()}.
 * 
 * @author <a href="mailto:haui@haumacher.de">Bernhard Haumacher</a>
 * @version $Revision: 16 $ $Author: haumacher@googlemail.com $ $Date: 2013-07-27 12:17:41 +0000 (Sat, 27 Jul 2013) $
 */
public abstract class AbstractObservable extends MarkableImpl implements Observable {

	private final Scope _scope;
	
	private final List<Observable> _users = new ArrayList<Observable>();

	/**
	 * Creates a {@link AbstractObservable}.
	 * 
	 * @param scope
	 *        See {@link #getScope()}.
	 */
	public AbstractObservable(Scope scope) {
		_scope = scope;
	}

	protected Scope getScope() {
		return _scope;
	}
	
	@Override
	public void invalidate() {
		_scope.registerInvalid(this);
	}
	
	@Override
	public List<Observable> getUsage() {
		return _users;
	}

	@Override
	public void registerUser(Observable user) {
		_users.add(user);
	}

	@Override
	public void unregisterUser(Observable user) {
		_users.remove(user);
	}
}
